package com.itzmn.newcode.offer;

import java.util.HashMap;

/**
 * @Author: 张梦楠
 * @Date: 2018/11/16 14:19
 * 简书：https://www.jianshu.com/u/d611be10d1a6
 * 码云：https://gitee.com/zhangqiye
 * @Description: 剑指Offer  ，打印出超过数组长度一半的数据
 */
public class MoreThanArrayNumHalf {


    public static void main(String[] args) {


    }
    public int MoreThanHalfNum_Solution(int [] array) {

        HashMap<Integer, Integer> map = new HashMap<>();

        int max =0;
        int half = array.length/2;
        for (int i:array){
            map.put(i,map.get(i)==null?1:map.get(i)+1);
        }

        for (HashMap.Entry entry:map.entrySet()){
            if ((Integer)entry.getValue() > half){
                max = (Integer)entry.getKey();
                break;
            }
        }
        return max;
    }

}
